﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gpsys.Entity.MSG;
using Gpsys.Logger;

namespace Gpsys.Web.Business
{
    /// <summary>
    /// PortBusiness
    /// </summary>
    public class PortBusiness : BaseBusiness
    {
        /// <summary>
        /// Updates the Port.
        /// </summary>
        /// <param name="port">The trans.</param>
        /// <returns></returns>
        public bool UpdatePort(Port port)
        {
            try
            {
                SQL.OpenConnection();

                SQL.Execute(@"UPDATE [Port]
                                               SET [Code] = @code
                                                  ,[Description] = @description
                                                  ,[Configuration] = @xml
                                                  ,[Type] = @type
                                                  ,[AdapterType] = @adapterType
                                                  ,[ModifiedBy] = @modifiedBy
                                                  ,[ModifiedOn] = getdate()
                                             WHERE [Id] = @Id",
                                                         new
                                                         {
                                                             Id = port.Id,
                                                             code = port.Code,
                                                             description = port.Description,
                                                             xml = port.Configuration,
                                                             type = port.Type,
                                                             adapterType = port.AdapterType,
                                                             modifiedBy = port.ModifiedBy
                                                         });

            }
            catch (Exception e)
            {
                Log.WriteError("UpdatePort : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }

        public bool DeletePort(int Id)
        {
            try
            {
                SQL.OpenConnection();

                SQL.Execute(@"UPDATE [Port]
                                               SET IsDeleted =  @IsDeleted
                                             WHERE [Id] = @Id",
                                                         new
                                                         {
                                                             Id = Id,
                                                             IsDeleted = true
                                                         });

            }
            catch (Exception e)
            {
                Log.WriteError("DeletePort : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }

        /// <summary>
        /// Gets the Ports.
        /// </summary>
        /// <returns></returns>
        public List<Port> GetPorts()
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Port>(@"SELECT [Id]
      ,[Code]
      ,[Description]
      ,[Type]
      ,[AdapterType]
      ,[Configuration]
      ,[CreatedBy]
      ,[CreatedOn]
      ,[ModifiedBy]
      ,[ModifiedOn]
      ,[IsDeleted]
                                              FROM [Port]
                                              Where IsDeleted = 0").ToList();

            }
            catch (Exception e)
            {
                Log.WriteError("GetPorts : " + e.Message);

                return new List<Port>();
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Gets the Port.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        public Port GetPort(int Id)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Port>(@"SELECT [Id]
      ,[Code]
      ,[Description]
      ,[Type]
      ,[AdapterType]
      ,[Configuration]
      ,[CreatedBy]
      ,[CreatedOn]
      ,[ModifiedBy]
      ,[ModifiedOn]
      ,[IsDeleted]
                                              FROM [Port]
                                              WHERE [Id] = @id", new { id = Id }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("GetPort : " + e.Message);

                return null;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Gets the Port.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public Port GetPort(string code)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Port>(@"SELECT [Id]
      ,[Code]
      ,[Description]
      ,[Type]
      ,[AdapterType]
      ,[Configuration]
      ,[CreatedBy]
      ,[CreatedOn]
      ,[ModifiedBy]
      ,[ModifiedOn]
      ,[IsDeleted]
                                              FROM [Port]
                                              WHERE [Code] = @code", new { code = code }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("GetPort : " + e.Message);

                return null;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Inserts the Port.
        /// </summary>
        /// <param name="port">The trans.</param>
        /// <returns></returns>
        public bool InsertPort(Port port)
        {
            try
            {
                SQL.OpenConnection();

                port.Id = SQL.Query<int>(@"INSERT INTO [Port]
                               ([Code]
           ,[Description]
           ,[Type]
           ,[AdapterType]
           ,[Configuration]
           ,[CreatedBy]
           ,[CreatedOn]
           ,[ModifiedBy]
           ,[ModifiedOn]
           ,[IsDeleted])
                         VALUES
                               ( @code,
                                 @description,
                                 @type,
                                 @adapterType,
                                 @xml,
                                 @createdBy,
                                 getdate(),
                                 @modifiedBy,
                                 getdate(),
                                 0);

                        SELECT CAST(SCOPE_IDENTITY() as int);",
                                                         new
                                                            {
                                                                code = port.Code,
                                                                description = port.Description,
                                                                type =  port.Type,
                                                                adapterType = port.AdapterType,
                                                                xml = port.Configuration,
                                                                createdBy = port.CreatedBy,
                                                                modifiedBy = port.ModifiedBy
                                                            }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("InsertPort : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }


        /// <summary>
        /// Checks the code used.
        /// </summary>
        /// <param name="trams">The trams.</param>
        /// <returns></returns>
        public bool CheckCodeUsed(Port trams)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<int>(@"Select count(1) from [Port] where [Code] = @code and [Id] != @Id",
                                                         new
                                                         {
                                                             Id = trams.Id,
                                                             code = trams.Code
                                                         }).SingleOrDefault() > 0;

            }
            catch (Exception e)
            {
                Log.WriteError("CheckCodeUsed : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

    }
}
